package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.example.entity.Article;
import com.example.entity.Focus;
import com.example.service.ArticalService;
import com.example.service.FocusService;
import com.example.util.BeanFactroy;
import com.example.util.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/focusServlet/*")
public class FocusController extends HttpServlet {
    private FocusService focusService = BeanFactroy.createBean(FocusService.class);
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        String path = request.getPathInfo();

        if ("/isFocus".equals(path)) {
            isFocus(request, response);
        }
        if ("/focus".equals(path)) {
            focus(request, response);
        }
        if ("/cancelFocus".equals(path)) {
            cancelFocus(request, response);
        }
        if ("/addfans".equals(path)) {
            addfans(request, response);
        }
        if ("/reducefans".equals(path)) {
            reducefans(request, response);
        }
    }

    //判断
    private void isFocus(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Long userId = Long.parseLong(request.getParameter("uid"));
        Long authorId = Long.parseLong(request.getParameter("aid"));
        Focus focus = focusService.isFocus(userId, authorId);
//        System.out.println("focus"+focus);
        if (focus==null){
            response.getWriter().print(JSON.toJSONString(R.ok()));
        }else {
            response.getWriter().print(JSON.toJSONString(R.error()));
        }
    }

    //关注
    private void focus(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Long userId = Long.parseLong(request.getParameter("uid"));
        Long authorId = Long.parseLong(request.getParameter("aid"));
        Focus focus = new Focus();
        focus.setUserId(userId);
        focus.setAuthorId(authorId);
        int res = focusService.focus(focus);
        if(res>0){
            //用户关注加1
            int i = focusService.updateFocus(userId);
            //如果返回的两个数据一样,后面输出的数据直接绑定前面输出的数据
            response.getWriter().print(JSON.toJSONString(R.ok().data("updataFocus",i)));
        }else{
            response.getWriter().print(JSON.toJSONString(R.error()));
        }
    }

    //取消关注
    private void cancelFocus(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Long userId = Long.parseLong(request.getParameter("uid"));
        Long authorId = Long.parseLong(request.getParameter("aid"));

        int i = focusService.cancelFocus(userId, authorId);
//        System.out.println(i);
        if(i>0){
            //用户关注-1
            int res = focusService.updateFocus(userId);
            response.getWriter().print(JSON.toJSONString(R.ok().data("updataFocus",res)));
        }else{
            response.getWriter().print(JSON.toJSONString(R.error()));
        }
    }

    //关注,作者加粉
    private void addfans(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Long userId = Long.parseLong(request.getParameter("aid"));
        //作者粉丝加1
        int addfans = focusService.addfans(userId);

        response.getWriter().print(JSON.toJSONString(R.ok().data("addfans",addfans)));
    }

    //取消关注,作者掉粉
    private void reducefans(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Long userId = Long.parseLong(request.getParameter("aid"));
        //作者粉丝加1
        int reducefans = focusService.reducefans(userId);

        response.getWriter().print(JSON.toJSONString(R.ok().data("reducefans",reducefans)));
    }

}
